import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.Executors;
import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @author zhangdi
 * @version 1.0
 * @date 2022.3.8 14:18
 */
public class ZDThreadPoolExecutor extends ScheduledThreadPoolExecutor {
    private Map<String, TestTask> taskMap = new HashMap<>();

    public ZDThreadPoolExecutor(int corePoolSize) {
        super(corePoolSize);
    }


//    public ZDThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, ThreadFactory threadFactory) {
//        super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, threadFactory);
//    }
//
//    public ZDThreadPoolExecutor(int corePoolSize,
//                                int maximumPoolSize,
//                                long keepAliveTime,
//                                TimeUnit unit,
//                                BlockingQueue<Runnable> workQueue,
//                                RejectedExecutionHandler handler, TestThread testThread) {
//        super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue,
//                Executors.defaultThreadFactory(), handler);
//        this.testThread = testThread;
//    }


    public void exec(TestTask command, long initialDelay, long delay, TimeUnit unit) {
        command.future(super.schedule(command, delay, unit));
        taskMap.put(command.getName(), command);
        System.out.println(command.getName() + " 加入线程队列");
    }

    public boolean stop(String name) {
        if (!taskMap.containsKey(name)) {
            return false;
        }

        TestTask task = taskMap.get(name);
        task.stop();

        return true;
    }
}
